const {nanoid}=require("nanoid")
const {sqlConnect}=require("../model/index")
function getRandom(length){
    return Math.round(Math.random()*length)
}
let arr= []
for (let first_num = 1; first_num < 11; first_num++) {
    let first_str=first_num.toString()
    if(first_str.length==1){
        first_str='0'+first_str
    }
    let second_str_set = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
    second_str_set.map((item) => {
        return arr.push(first_str+ item)
    })
}
let sql1=`select id from person`
function change_p_id_arr(p_id_obj){
    let p_id_arr=p_id_obj.map((elem)=>{
        return elem.id
    })
    return p_id_arr
}
var p_id_arr=[]
function db(sql){
    return new Promise(function(resolve,reject){
        sqlConnect.query(sql,function(err,dbres){
            if(err){
                reject(err)
            }
            else{
                resolve(dbres)
            }
        })
    })
}
async function search_p_id(sql1){
    await db(sql1).then(dbres=>{
        p_id_arr=change_p_id_arr(dbres)
        return p_id_arr
    })
}
p_id_arr=search_p_id(sql1).then(
    ()=>{
        //console.log(p_id_arr)
    }
)
let sql2=`select id from airline_info`
let a_id_arr=[]
sqlConnect.query(sql2,function(err,a_id_obj){
    if(err)return console.log(err)
    else {a_id_arr=a_id_obj.map((elem)=>{
        return elem.id
    })
    //console.log(a_id_arr)
    return a_id_arr
    }
})
setTimeout(()=>{console.log(a_id_arr)},1000)
let data=[]
setTimeout(()=>{
    for(let t=0;t<10;t++){
        let p_id=p_id_arr[getRandom(904)]
        let a_id="ShBEn54Hqo"
        let seat_no=arr[getRandom(99)]
        let id=nanoid(10)
        data.push([id,a_id,p_id,seat_no])
    }
    let sql3=`insert into seat (id,a_id,p_id,seat_no) values ?`
    sqlConnect.query(sql3,[data],function(err){
        if(err)return console.log(err)
        else return console.log('success')
    })
},5000)


